package com.hzit.ms.controller;

import com.hzit.ms.resp.RespVo;
import com.hzit.ms.resp.SkGoodsDetailData;
import com.hzit.ms.service.IGoodsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 商品相关控制器
 */
@Controller
@RequestMapping("/goods")
public class GoodsController {

    private Logger logger = LoggerFactory.getLogger(GoodsController.class);

    @Autowired
    private IGoodsService goodsService;

    /**
     * 显示秒杀商品列表
     */
    @RequestMapping("/list")
    public String showGoodsList(Model model){
        logger.info("###########加载秒杀商品列表########");
        //写sql语句查询
        model.addAttribute("goodsList",goodsService.getSkillGoodsList());
        return "goods_list";
    }


    /**
     * 获取秒杀商品详情
     * @param goodsId
     * @return
     */
    @RequestMapping("/detail")
    @ResponseBody
    public RespVo<SkGoodsDetailData> getSkGoodsDetail(@RequestParam("goodsId") String goodsId){

        logger.info("###########获取商品id：{}，详细信息。########",goodsId);


        RespVo<SkGoodsDetailData>  respVo = new RespVo<SkGoodsDetailData> ();

        if (StringUtils.isEmpty(goodsId)){
            respVo.setCode(-1);
            respVo.setMsg("商品id为空");
            return respVo;
        }

        SkGoodsDetailData skGoodsDetailData = goodsService.getSkillGoodsDetail(goodsId);

        respVo.setCode(0);
        respVo.setMsg("成功");
        respVo.setData(skGoodsDetailData);
        return respVo;

    }

}
